package com.anche.cywebnew.Service.systemManager.Impl;

import com.anche.cywebnew.DAO.systemManager.CyUserMapper;
import com.anche.cywebnew.Pojo.systemManager.CyUser;
import com.anche.cywebnew.Pojo.systemManager.SysUser;
import com.anche.cywebnew.Service.systemManager.CyUserService;
import com.anche.cywebnew.Service.systemManager.OrganService;
import com.anche.cywebnew.Utils.annotation.OptLog;
import com.anche.cywebnew.Utils.enums.OptType;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.FileCopyUtils;
import sun.misc.BASE64Decoder;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Service
public class CyUserServiceImpl implements CyUserService {

    @Autowired
    private CyUserMapper cyUserMapper;

    @Autowired
    private OrganService organService;

    @Override
    public Page<CyUser> getCyuserPager(Integer page, Integer limit, CyUser cyUser) {
        SysUser currUser = (SysUser) SecurityUtils.getSubject().getPrincipal();   // 获取当前登录用户信息
        List<String> cyqxhs = null;
        if (currUser.getUsertype() != 0) {
            cyqxhs = organService.getListByCyqxh(currUser.getOrgan());   // 获取用户的所属机构以及以下子机构的cyqxh
        }

        Page<CyUser> page1 = PageHelper.startPage(page, limit);
        cyUserMapper.selectCyUserList(cyUser, cyqxhs);
        return page1;
    }

    @Override
    public List<CyUser> getCyuserList() {
        return cyUserMapper.selectCyUserList(new CyUser(), null);
    }

    @Override
    public CyUser getCyuser(String cyysfzhm) {
        CyUser cyUser = new CyUser();
        cyUser.setCyysfzhm(cyysfzhm);
        cyUser = cyUserMapper.selectOne(cyUser);
        return cyUser;
    }

    @Override
    public void addCyUser(CyUser cyUser, String strBase64) throws Exception {
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        cyUser.setId(uuid);
        if (strBase64 != null && !strBase64.equals("")) {
            byte[] bytes = base64ToIO(strBase64);
            cyUser.setHeadimg(bytes);
        }
        cyUserMapper.insertCyUser(cyUser);
    }

    @Override
    public void setCyuserInfo(CyUser cyUser, String strBase64) throws Exception {
        if (strBase64 != null && !strBase64.equals("")) {
            byte[] bytes = base64ToIO(strBase64);
            cyUser.setHeadimg(bytes);
        }
        cyUserMapper.updateById(cyUser);
    }

    @Override
    public void delCyuserInfo(String id) {
        cyUserMapper.deleteById(id);
    }

    private byte[] base64ToIO(String base64) throws IOException {
        ByteArrayInputStream stream = null;
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] bytes1 = decoder.decodeBuffer(base64);
        stream = new ByteArrayInputStream(bytes1);
        return FileCopyUtils.copyToByteArray(stream);//得到byte
    }

}
